//
//  custom.m
//  PaoPao
//
//  Created by Apple on 12/23/15.
//  Copyright © 2015 Apple. All rights reserved.
//

#import "custom.h"

#define KCellHeight         44
#define kArrorHeight        10
#define KHorizontalSpace    40
#define KHorizontalWidth    170

@implementation custom

-(id)initArray:(NSArray *)array andPosition:(CGPoint) point{
    if(self=[super init]){
        if(point.x<100){
            self.frame=CGRectMake(point.x-KHorizontalSpace, point.y+kArrorHeight+10, KHorizontalWidth, array.count*KCellHeight);
            _flag=true;
        }else if (point.x>[UIScreen mainScreen].bounds.size.width-100){
            self.frame=CGRectMake(point.x-KHorizontalWidth, point.y+kArrorHeight+10, KHorizontalWidth, array.count*KCellHeight);
            _flag=false;
        }
    }
    self.array=[NSArray arrayWithArray:array];
    return self;
}


- (void)drawRect:(CGRect)rect {
    [self drawInContext:UIGraphicsGetCurrentContext()];
    //阴影颜色
    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}
- (void)drawInContext:(CGContextRef)context
{
    
    CGContextSetLineWidth(context, 2.0);
    //CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.8].CGColor);
    
    //arrow颜色
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    
    [self getDrawPathUp:context];
    CGContextFillPath(context);
    
    UITableView *tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, kArrorHeight, self.frame.size.width, self.frame.size.height-kArrorHeight)];
    [self addSubview:tableview];
    tableview.delegate=self;
    tableview.dataSource=self;

}
- (void)getDrawPathDown:(CGContextRef)context
{
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

-(void)getDrawPathUp:(CGContextRef)context{
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat maxy=CGRectGetMaxY(rrect);
    CGFloat miny=CGRectGetMinY(rrect);
    //CGFloat midy=CGRectGetMidY(rrect);
    
    CGFloat maxx=CGRectGetMaxX(rrect);
    CGFloat minx=CGRectGetMinX(rrect);
    CGFloat midx;
    if(_flag){
       midx=CGRectGetMidX(rrect)-KHorizontalSpace;
    }else{
        midx=CGRectGetMidX(rrect)+KHorizontalSpace;
    }
    
    
//    CGFloat minx = CGRectGetMinX(rrect),
//    midx = CGRectGetMidX(rrect),
//    maxx = CGRectGetMaxX(rrect);
//    CGFloat miny = CGRectGetMinY(rrect),
//    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
//    
    CGContextMoveToPoint(context, midx-kArrorHeight, miny+kArrorHeight);
    CGContextAddLineToPoint(context,midx, miny);
    CGContextAddLineToPoint(context,midx+kArrorHeight, miny+kArrorHeight);
//
    CGContextAddArcToPoint(context, maxx, miny+kArrorHeight, maxx, maxy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, minx, maxy, radius);
    CGContextAddArcToPoint(context, minx, maxy, minx, miny+kArrorHeight, radius);
    CGContextAddArcToPoint(context, minx, miny+kArrorHeight, midx-kArrorHeight, miny+kArrorHeight, radius);
    CGContextClosePath(context);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"cell";
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    //UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.textLabel.text=[self.array objectAtIndex:indexPath.row];
    cell.imageView.image=[UIImage imageNamed:@"label"];
    return cell;
}
@end
